/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;
import com.cmcc.zysoft.infocollection.model.Industry;
import com.cmcc.zysoft.infocollection.model.ResourceProperty;
import com.starit.common.dao.hibernate.HibernateBaseDaoImpl;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: ResourcePropertyPcDao.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-8-14 下午4:13:41
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Repository
public class ResourcePropertyPcDao extends HibernateBaseDaoImpl<ResourceProperty, String> {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 根据行业ID获取资源属性分页列表信息.
	 * @param industryId 行业ID
	 * @param page
	 * @param rows
	 * @return map
	 */
	public Map<String, Object> getPropertyList(String industryId, int page, int rows) {
		int offset = (page - 1) * rows;
		Map<String, Object> map = new HashMap<String, Object>();
		//模板设置中'区县'、'服务项目'、'商户名称'、'联系电话'、'经度'、'维度'不显示
		String rowSql = 
				" SELECT a.resource_property_id AS resourcePropertyId, a.resource_property_name AS resourcePropertyName, " +
				" a.resource_property_enname AS resourcePropertyEnname, a.resource_property_flag AS resourcePropertyFlag, " +
				" a.mark1 AS mark1, a.mark2 AS mark2, a.mark3 AS mark3, a.mark4 AS mark4 " +
				" FROM tb_c_resource_property a " +
				" WHERE a.industry_id = ? " +
				" AND a.resource_property_enname NOT IN ('caterReg', 'caterServ', 'caterName', 'caterMobile', 'x', 'y') " +
				" LIMIT ?, ? ";
		String countSql = 
				" SELECT COUNT(*) FROM tb_c_resource_property a WHERE a.industry_id = ? " +
				" AND a.resource_property_enname NOT IN ('caterReg', 'caterServ', 'caterName', 'caterMobile', 'x', 'y') ";
		List<Map<String, Object>> list = this.jdbcTemplate.queryForList(rowSql, industryId, offset, rows);
		int total = this.jdbcTemplate.queryForInt(countSql, industryId);
		map.put("rows", list);
		map.put("total", total);
		return map;
	}
	
	/**
	 * 检查同一行业下资源属性名称是否已存在.
	 * @param industryId 行业ID
	 * @param propertyName 资源属性名称
	 * @param propertyId 资源属性ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> checkPropertyName(String industryId, String propertyName, String propertyId) {
		String sql = "SELECT a.* FROM tb_c_resource_property a WHERE a.industry_id = ? AND a.resource_property_name = ?";
		if(StringUtils.hasText(propertyId)) {
			sql += " AND a.resource_property_id != '" + propertyId + "'";
		}
		return this.jdbcTemplate.queryForList(sql, industryId, propertyName);
	}
	
	/**
	 * 检查同一行业下资源属性代码是否已存在.
	 * @param industryId 行业ID
	 * @param propertyCode 资源属性代码
	 * @param propertyId 资源属性ID
	 * @return List<Map<String, Object>>
	 */
	public List<Map<String, Object>> checkPropertyCode(String industryId, String propertyCode, String propertyId) {
		String sql = "SELECT a.* FROM tb_c_resource_property a WHERE a.industry_id = ? AND a.resource_property_enname = ?";
		if(StringUtils.hasText(propertyId)) {
			sql += " AND a.resource_property_id != '" + propertyId + "'";
		}
		return this.jdbcTemplate.queryForList(sql, industryId, propertyCode);
	}
	
	/**
	 * 根据行业信息和资源属性代码查找资源属性信息.
	 * @param industry 行业信息
	 * @param resourcePropertyEnname 资源属性代码
	 * @return ResourceProperty
	 */
	public ResourceProperty getResourceProperty(Industry industry, String resourcePropertyEnname) {
		List<ResourceProperty> list = this.findByNamedParam(new String[]{"industry", "resourcePropertyEnname"}, new Object[]{industry, resourcePropertyEnname});
		if(null != list && !list.isEmpty()) {
			return list.get(0);
		} else {
			return null;
		}
	}
}